package test3_11;

import java.util.Arrays;

public class Test_zeroArray {
    public static void main(String[] args) {
        /*
        把数组中的非零元素放在新数组中
        v={3,2,4,5,7,0,0,0,6,0,9,0,8} => {3,2,4,5,7,6,9,8}
     */

        int[] v = {3, 2, 4, 5, 7, 0, 0, 0, 6, 0, 9, 0, 8};
        int sum = 0;
        for (int i = 0; i < v.length; i++) {
            if (v[i] != 0) {
                sum += 1;
            }
        }
        int[] v_new = new int[sum];
        int index = 0;
        for (int i = 0; i < v.length; i++) {
            if (v[i] != 0) {
                v_new[index] = v[i];
                index++;
            }
        }
        System.out.println(Arrays.toString(v_new));

        int[] v_new2 = new int[v.length];
        for (int i = 0; i < v.length; i++) {
            if (i < v_new.length) {
                v_new2[i] = v_new[i];
            } else {
                v_new2[i] = 0;
            }
        }
        System.out.println(Arrays.toString(v_new2));
    }
}
